package com.fanghaoran.view.user.viewFrame;

import com.fanghaoran.controller.BrowseController;
import com.fanghaoran.entity.InfoToDisplay;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author: win11
 * @Date: 2023/03/31/0:54
 * @Description: 排行榜页面
 */
public class RankFrame extends JFrame {
    public RankFrame() throws Exception {
        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView();
        //设置可见
        this.setVisible(true);
    }

    private void initView() throws Exception {
        //设置分隔面板
        JSplitPane sp = new JSplitPane();
        //设置支持连续布局
        sp.setContinuousLayout(false);
        sp.setEnabled(false);
        //设置分割线位置
        sp.setDividerLocation(250);
        //设置分割线大小
        sp.setDividerSize(5);

        //左边部分
        JPanel panel1=new JPanel();
        JLabel title=new JLabel("单词榜");
        //展示信息的表格
        //数据库获取所有用户信息
        @SuppressWarnings("unchecked")
        ArrayList<InfoToDisplay> dataList = (ArrayList<InfoToDisplay>)new BrowseController().getWordRank().getMessage();
        JTable table1 = new JTable(new WordRankTableModel(dataList));
        table1.setColumnSelectionAllowed(false);
        table1.setRowSelectionAllowed(false);
        table1.setPreferredScrollableViewportSize(table1.getPreferredSize());
        table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //放在panel
        panel1.add(title);
        panel1.add(new JScrollPane(table1));

        //右边部分
        JPanel panel2=new JPanel();
        JLabel title2=new JLabel("连续打卡天数榜");
        //展示信息的表格
        //数据库获取所有用户信息
        @SuppressWarnings("unchecked")
        ArrayList<InfoToDisplay> dataList2 = (ArrayList<InfoToDisplay>)new BrowseController().getClockDay().getMessage();
        JTable table2 = new JTable(new ClockRankTableModel(dataList2));
        table2.setColumnSelectionAllowed(false);
        table2.setRowSelectionAllowed(false);
        table2.setPreferredScrollableViewportSize(table2.getPreferredSize());
        table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //放在panel
        panel2.add(title2);
        panel2.add(new JScrollPane(table2));

        sp.setLeftComponent(panel1);
        sp.setRightComponent(panel2);
        this.add(sp);
    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(580, 580);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}
